package com.bkgy.controller.plateThickAbnormal.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.controller.plateThickAbnormal.entity.tDefectData;
import com.bkgy.controller.plateThickAbnormal.entity.tDefectDataRequstCondition;
import com.bkgy.controller.plateThickAbnormal.mapper.DefectDataExcelMapper;
import com.bkgy.controller.plateThickAbnormal.mapper.plateThickAbnormalMapper;
import com.bkgy.controller.plateThickAbnormal.service.plateThickAbnormalExcel;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class plateThickAbnormalExcelImpl extends ServiceImpl<DefectDataExcelMapper, tDefectData> implements plateThickAbnormalExcel {
    @Resource
    private HttpServletResponse response;

    @Autowired
    plateThickAbnormalMapper plateThickAbnormalMapper;

    /**
     * 将日期格式由YYYY-MM-DD HH:MM:SS 转为 YYYYMMDDHHSSMM
     * @param Date_str YYYY-MM-DD HH:MM:SS
     * @return YYYYMMDDHHSSMM
     */
    public String constructL3Date(String Date_str) {
        String l3Date = "";//时间格式：20250703075254
        try {
            String[] array_Time = Date_str.split("-");//时间格式：2025-06-01 00:00:01
            //构建时间格式：20250703075254
            String times2 = array_Time[2].replace(" ", "");
            times2 = times2.replace(":", "");
            l3Date = array_Time[0] + array_Time[1] + times2;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l3Date;
    }

    @Override
    public void exportplateThick(QueryDTO<tDefectDataRequstCondition> queryDTO)
    {
        //日期转换
        tDefectDataRequstCondition condition = queryDTO.getDto();

        String dbegin_str = constructL3Date(condition.getDbegin());
        String dend_str =constructL3Date(condition.getDend());
        condition.setDbegin(dbegin_str);
        condition.setDend(dend_str);
        List<tDefectData> dataList=plateThickAbnormalMapper.tdefectData_Excel(condition);
        try {
            // 设置导出参数
            ExportParams exportParams = new ExportParams("酸轧厚度、版型异常明细", "酸轧厚度、版型异常明细", ExcelType.XSSF);

            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, tDefectData.class, dataList);

            // 行列固定
            if (workbook.getNumberOfSheets() > 0) {
                workbook.getSheetAt(0).createFreezePane(1, 2);
            }

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("酸轧厚度、版型异常明细", StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException("导出Excel失败", e);
        }
    }

}

